﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using Microsoft.Practices.Prism.Logging;
using Microsoft.Practices.Prism.Modularity;
using Microsoft.Practices.ServiceLocation;
using Castle.MicroKernel.Registration;
using Microsoft.Practices.Prism.Regions;
using Tanmia.Basis.Prism;
using Tanmia.Basis.Data.NHibernate;
using Tanmia.Basis.ViewInterface;
using Tanmia.Basis.Data;
using Tanmia.Basis.ServiceInterface;
using Tanmia.Basis.Concretes;
using System.Reflection;
using System.Configuration;
using Tanmia.Basis.Security;
using Tanmia.Basis.Security.Adapter;
using SaryaAccounting.UserSelection;

namespace SaryaAccounting
{
    public class AppBootstrapper : WindsorBootstrapper
    {
        public AppBootstrapper()
            : base(new AppSplashScreen())
        {
        }

        protected virtual void ConfigureDatabases()
        {
            string appDbRep = ConfigurationManager.AppSettings["AppDbRep"];
            bool generateApplicationDatabase = Convert.ToBoolean(ConfigurationManager.AppSettings["GAD"]);

            DatabaseConfiguration dbConfig = new DatabaseConfiguration("NHibernate.config", Assembly.Load(appDbRep));
            dbConfig.BuildSessionFactory();
            if (generateApplicationDatabase)
                dbConfig.GenerateDatabase();
            Container.Register(Component.For<DatabaseConfiguration>().Instance(dbConfig).Named("AppDb").LifeStyle.Singleton);


            string appSecAdapter = ConfigurationManager.AppSettings["AppSecAdapter"];
            bool generateSecurityDatabase = Convert.ToBoolean(ConfigurationManager.AppSettings["GSD"]);

            DatabaseConfiguration secConfig = new DatabaseConfiguration("Security.config", Assembly.Load(appSecAdapter));
            SecurityAdapterInitializer.Init(secConfig);
            secConfig.BuildSessionFactory();
            if (generateSecurityDatabase)
                secConfig.GenerateDatabase();
            SecurityAdapterInitializer.ConfigureContainer(secConfig, Container);
            SecurityAdapterInitializer.SetupOperation(Container);

            Container.Register(Component.For<DatabaseConfiguration>().Instance(secConfig).Named("SecDb").LifeStyle.Singleton);
        }

        protected override void ConfigureContainer()
        {
            base.ConfigureContainer();

            ConfigureDatabases();
            
            RegisterTypeIfMissing(typeof(IMessageService), typeof(MessageService), true);
            RegisterTypeIfMissing(typeof(IUserSelector), typeof(UserViewModel), true);
            RegisterTypeIfMissing(typeof(MainWindowViewModel), typeof(MainWindowViewModel), false);
            RegisterTypeIfMissing(typeof(IShellView), typeof(MainWindowView), true);
            RegisterTypeIfMissing(typeof(ISessionManager), typeof(SessionManager), false);
            RegisterTypeIfMissing(typeof(INavigationService), typeof(NavigationService), true);
        }

        protected override void InitializeShell()
        {
            base.InitializeShell();

            // We have to set the main window here, for ShutdownMode running in a correct state.
            Application.Current.MainWindow = Shell as Window;
            //Application.Current.MainWindow.Style = Application.Current.FindResource("WindowStyle") as Style;
            Application.Current.MainWindow.Show();
        }

        protected override DependencyObject CreateShell()
        {
            return Container.Resolve<IShellView>() as DependencyObject;
        }
    }
}
